package com.zbc.controller.admin;

import com.zbc.dto.CategoryDTO;
import com.zbc.dto.CategoryPageDTO;
import com.zbc.entity.Category;
import com.zbc.result.PageResult;
import com.zbc.result.Result;
import com.zbc.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/category")
@Api(tags = "分类管理接口")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("page")
    @ApiOperation("获取分类页面信息")
    public Result<PageResult> fetchPageList(CategoryPageDTO categoryPageDTO){
        PageResult pageResult = categoryService.getPageCategory(categoryPageDTO);
        return Result.success(pageResult);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("切换分类状态")
    public Result switchStatus(@PathVariable Integer status, Integer id){
            categoryService.switchCategoryStatus(status, id);
            return Result.success();
    }

    @PostMapping()
    @ApiOperation("添加分类")
    public Result addCategory(@RequestBody CategoryDTO categoryDTO){
        categoryService.saveCategory(categoryDTO);
        return Result.success();
    }

    @PutMapping()
    @ApiOperation("修改分类信息")
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO){
        categoryService.updateCategory(categoryDTO);
        return Result.success();
    }

    @DeleteMapping()
    @ApiOperation("删除分类信息")
    public Result deleteCategory(Integer id){
        categoryService.deleteCategory(id);
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("查询所用分类列表")
    public Result<Category[]> list(Integer type){
        Category[] categories = categoryService.fetchTypeList(type);
        return Result.success(categories);
    }
}
